

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Header
#pragma once
#include "UniBase.h"
#include <PIMStore.h>

#ifndef FMCF_FINDPHONE
#define FMCF_FINDPHONE 0x00000001
#endif
typedef HRESULT (*PFindMatchingContact)(IPOutlookApp*, PCWSTR, DWORD dwFlags, IUnknown**, CEPROPID*);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// MatchCtt class
class MatchCtt
{
protected:
	IFolder* m_pFolder;
	IPOutlookApp* m_pOutlook;
	IPOutlookItemCollection* m_pItems;
	PFindMatchingContact pFindMatchingContact;

public:
	// Constructor
	MatchCtt()
	{
		m_pFolder = NULL;
		m_pOutlook = NULL;
		m_pItems = NULL;
		CoCreateInstance(CLSID_Application, NULL, CLSCTX_INPROC_SERVER, IID_IPOutlookApp, (PVOID*) &m_pOutlook);
		if (m_pOutlook)
		{
			m_pOutlook->Logon(NULL);

			// Get FindMatchingContact function (only valid in WM5 or later)
			pFindMatchingContact = (PFindMatchingContact) GetProcAddress(GetModuleHandle(TEXT("PIMStore")), TEXT("FindMatchingContact"));
			if (pFindMatchingContact == NULL)
			{
				m_pOutlook->GetDefaultFolder(olFolderContacts, &m_pFolder);
				if (m_pFolder)
				{
					m_pFolder->get_Items(&m_pItems);
				}
			}
		}
	}

	// Find contact name by phone number in Windows Mobile Contacts Database
	UINT FindNameByPhone(PCWSTR pwzPhone, PWSTR pwzName)
	{
		IContact* pContact = NULL;
		if (pFindMatchingContact)
		{
			// WM5 or later: use FindMatchingContact to do this 
			CEPROPID idProp;
			IUnknown* pItem = NULL;
			pFindMatchingContact(m_pOutlook, pwzPhone, FMCF_FINDPHONE, &pItem, &idProp);
			if (pItem)
			{
				pItem->QueryInterface(IID_IContact, (PVOID*) &pContact);
			}
		}
		else if (m_pItems)
		{
			// SP/PPC 2003: query contacts by ourself
			WCHAR wzFind[MAX_PATH];
			WStrFormat(wzFind,
				L"[MobileTelephoneNumber] = \"%s\" OR [MobileTelephoneNumber] = \"%s\""
				L"[CarTelephoneNumber] = \"%s\" OR [CarTelephoneNumber] = \"%s\""
				L"[HomeTelephoneNumber] = \"%s\" OR [HomeTelephoneNumber] = \"%s\""
				L"[Home2TelephoneNumber] = \"%s\" OR [Home2TelephoneNumber] = \"%s\""
				L"[RadioTelephoneNumber] = \"%s\" OR [RadioTelephoneNumber] = \"%s\""
				L"[BusinessTelephoneNumber] = \"%s\" OR [BusinessTelephoneNumber] = \"%s\""
				L"[Business2TelephoneNumber] = \"%s\" OR [Business2TelephoneNumber] = \"%s\""
				L"[AssistantTelephoneNumber] = \"%s\" OR [AssistantTelephoneNumber] = \"%s\"",
				pwzPhone, &pwzPhone[3], pwzPhone, &pwzPhone[3],
				pwzPhone, &pwzPhone[3], pwzPhone, &pwzPhone[3],
				pwzPhone, &pwzPhone[3], pwzPhone, &pwzPhone[3],
				pwzPhone, &pwzPhone[3], pwzPhone, &pwzPhone[3]);
			m_pItems->Find(wzFind, (IDispatch**) &pContact);
		}

		// Nothing found
		if (pContact == NULL)
		{
			return 0;
		}

		// Got it, copy new name
		BSTR sName;
		pContact->get_FileAs(&sName);
		UINT nLen = WStrCopyN(pwzName, sName, MAX_NAME);
		pContact->Release();
		return nLen;
	}

	// Destructor
	~MatchCtt()
	{
		_SafeRelease(m_pItems);
		_SafeRelease(m_pFolder);
		if (m_pOutlook)
		{
			m_pOutlook->Logoff();
		}
		_SafeRelease(m_pOutlook);
	}
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
